package com.paishen.common.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.paishen.common.LibCommon;

import java.util.Set;

public class SpfsUtil {

    public static String ROOT_NAME = "app";

    public static void write( String key, Object value) {
        if(LibCommon.appContext() == null) {
            LogUtil.e("SpfsUtil write context empty");
            return ;
        }

        SharedPreferences spfs = LibCommon.appContext().getApplicationContext().getSharedPreferences(ROOT_NAME, Context.MODE_PRIVATE);
        Editor editor = spfs.edit();
        if (value instanceof String ) {
            editor.putString(key, (String) value);
        } else if ( value instanceof Double) {
            editor.putString(key, String.valueOf(((Double) value).doubleValue()));
        } else if (value instanceof Integer) {
            editor.putInt(key, (Integer) value);
        } else if (value instanceof Boolean) {
            editor.putBoolean(key, (Boolean) value);
        } else if (value instanceof Float) {
            editor.putFloat(key, (Float) value);
        } else if (value instanceof Long) {
            editor.putLong(key, (Long) value);
        } else {
            throw new IllegalArgumentException("not support type");
        }
        editor.commit();
    }

    public static String readString(String key) {
        return readString(key, null);
    }

    public static String readString( String key, String defValue) {
        if(LibCommon.appContext() == null) {
            LogUtil.e("SpfsUtil readString context empty");
            return "";
        }

        SharedPreferences spfs = LibCommon.appContext().getApplicationContext().getSharedPreferences(ROOT_NAME, Context.MODE_PRIVATE);
        return spfs.getString(key, defValue);
    }

    public static int readInt(String key) {
        return readInt(key, 0);
    }

    public static int readInt( String key, int defValue) {
        if(LibCommon.appContext() == null) {
            LogUtil.e("SpfsUtil readInt context empty");
            return 0;
        }
        SharedPreferences spfs = LibCommon.appContext().getApplicationContext().getSharedPreferences(ROOT_NAME, Context.MODE_PRIVATE);
        return spfs.getInt(key, defValue);
    }

    public static float readFloat( String key) {
        return readFloat(key, 0f);
    }

    public static float readFloat( String key, float defValue) {
        if(LibCommon.appContext() == null) {
            LogUtil.e("SpfsUtil readFloat context empty");
            return 0;
        }
        SharedPreferences spfs = LibCommon.appContext().getApplicationContext().getSharedPreferences(ROOT_NAME, Context.MODE_PRIVATE);
        return spfs.getFloat(key, defValue);
    }

    public static boolean readBoolean( String key) {
        return readBoolean( key, false);
    }

    public static boolean readBoolean( String key, boolean defValue) {
        if(LibCommon.appContext() == null) {
            LogUtil.e("SpfsUtil readBoolean context empty");
            return false;
        }
        SharedPreferences spfs = LibCommon.appContext().getApplicationContext().getSharedPreferences(ROOT_NAME, Context.MODE_PRIVATE);
        return spfs.getBoolean(key, defValue);
    }

    public static long readLong( String key) {
        return readLong( key, 0l);
    }

    public static long readLong( String key, long defValue) {
        if(LibCommon.appContext() == null) {
            LogUtil.e("SpfsUtil readLong context empty");
            return -1l;
        }
        SharedPreferences spfs = LibCommon.appContext().getApplicationContext().getSharedPreferences(ROOT_NAME, Context.MODE_PRIVATE);
        return spfs.getLong(key, defValue);
    }

    public static Set<String> readStringSet( String key) {
        return readStringSet( key, null);
    }

    public static Set<String> readStringSet( String key, Set<String> defValues) {
        if(LibCommon.appContext() == null) {
            LogUtil.e("SpfsUtil readStringSet context empty");
            return null;
        }
        SharedPreferences spfs = LibCommon.appContext().getApplicationContext().getSharedPreferences(ROOT_NAME, Context.MODE_PRIVATE);
        return spfs.getStringSet(key, defValues);
    }

    public static boolean contains(String key) {
        if(LibCommon.appContext() == null) {
            LogUtil.e("SpfsUtil contains context empty");
            return false;
        }
        SharedPreferences spfs = LibCommon.appContext().getApplicationContext().getSharedPreferences(ROOT_NAME, Context.MODE_PRIVATE);
        return spfs.contains(key);
    }

    public static boolean remove( String key) {
        if(LibCommon.appContext() == null) {
            LogUtil.e("SpfsUtil remove context empty");
            return false;
        }
        SharedPreferences spfs = LibCommon.appContext().getApplicationContext().getSharedPreferences(ROOT_NAME, Context.MODE_PRIVATE);
        return spfs.edit().remove(key).commit();
    }

    public boolean clear() {
        if(LibCommon.appContext() == null) {
            LogUtil.e("SpfsUtil remove context empty");
            return false;
        }
        SharedPreferences spfs = LibCommon.appContext().getApplicationContext().getSharedPreferences(ROOT_NAME, Context.MODE_PRIVATE);
        return spfs.edit().clear().commit();
    }
}
